{extend name="common/adminBase" /}
{block name="body"}
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">

                <div class="layui-card-header">
                    <button class="layui-btn  layui-btn-xs"  onclick="xadmin.open('添加菜单','addMenu',800,600)" ><i class="layui-icon">&#xe642;</i>添加菜单</button>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th width="70">ID</th>
                            <th>栏目名</th>
                            <th>图标</th>
                            <th width="50">排序</th>
                            <th width="80">状态</th>
                            <th width="250">操作</th>
                        </thead>
                        <tbody class="x-cate">
                        {volist name="menu" id="v"}
                        <tr cate-id="{$v.id}" fid="{$v.pid}" >
                            <td>{$v.id}</td>
                            <td>
                                <i class="layui-icon x-show" status='true'>&#xe623;</i>
                                <?php echo $v['title']?>
                            </td>
                            <td> <i class="iconfont"><?php echo $v['icon']?></i></td>
                            <td>{$v.sort}</td>
                            <td>

                                {if $v.status == 1}
                                <input type="checkbox" name="switch" value="{$v.id}" lay-skin="switch" lay-filter="switchTest"  checked >
                                {else/}
                                <input type="checkbox" name="switch" value="{$v.id}" lay-skin="switch" lay-filter="switchTest" >
                                {/if}

                            </td>
                            <td class="td-manage">
                                <a class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑','editMenu/?id={$v.id}',800,600)" >
                                    <i class="layui-icon">&#xe642;</i>编辑
                                </a>

                                <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="member_del({$v.id})" href="javascript:;" >
                                <i class="layui-icon">&#xe640;</i>删除
                                </button>
                            </td>
                        </tr>
                        {/volist}
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">

                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form'], function(){
        form = layui.form;

        form.on('switch(switchTest)', function (data) {
            console.log(data.elem.checked); //开关是否开启，true或者false
            console.log(data.value); //开关value值，也可以通过data.elem.value得到
            $.ajax({
                url:"changeStatus",
                data:{id:data.value,status:data.elem.checked},
                type:'post',
                dataType: 'json',
                success:function (res) {
                    if (res.status ==1){
                        layer.msg(res.msg,{icon:1,time:1500},function () {
                            location.reload();
                        });
                    }else{
                        layer.alert(res.msg,function (index) {
                            layer.close(index);
                        })
                    }
                }
            })


            return false;
        });

    });

    /*用户-删除*/
    function member_del(id){
        layer.confirm('确认要删除吗？',function(index){
            $.ajax({
                url:'delMenu',
                data:{id:id},
                type:'post',
                dataType:'json',
                success:function (res) {
                    if (res.status ==1){
                        layer.msg(res.msg,{icon:1,time:1500},function () {
                            location.reload();
                        });
                    }else{
                        layer.alert(res.msg,function (index) {
                            layer.close(index);
                        })
                    }
                }
            })

        });
    }

    function changeStatus(id){
        alert(id);
    }

    // 分类展开收起的分类的逻辑
    //
    $(function(){
        $("tbody.x-cate tr[fid!='0']").hide();
        // 栏目多级显示效果
        $('.x-show').click(function () {
            if($(this).attr('status')=='true'){
                $(this).html('&#xe625;');
                $(this).attr('status','false');
                cateId = $(this).parents('tr').attr('cate-id');
                $("tbody tr[fid="+cateId+"]").show();
            }else{
                cateIds = [];
                $(this).html('&#xe623;');
                $(this).attr('status','true');
                cateId = $(this).parents('tr').attr('cate-id');
                getCateId(cateId);
                for (var i in cateIds) {
                    $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
                }
            }
        })
    })

    var cateIds = [];
    function getCateId(cateId) {
        $("tbody tr[fid="+cateId+"]").each(function(index, el) {
            id = $(el).attr('cate-id');
            cateIds.push(id);
            getCateId(id);
        });
    }

</script>
</body>
{/block}